package com.huluobo.a2202mvvm_im_douyindemo.addGroup;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.BaseRepo;
import com.hyphenate.EMCallBack;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMCursorResult;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMGroupInfo;
import com.hyphenate.chat.EMGroupOptions;

import java.util.List;

/**
 * Created by LC on 2024/4/24.
 */
public class AddGroupRepo extends BaseRepo {
    /**
     * 创建群
     *
     * @param groupName  群名
     * @param desc       群描述
     * @param allMembers 首次创建群成员
     * @param reason     邀请进群的请求
     * @param option     设置
     */
    public void createGroup(
            String groupName,
            String desc,
            String[] allMembers,
            String reason,
            EMGroupOptions option, MutableLiveData<String> success, MutableLiveData<String> failed) {
        emClient.groupManager().asyncCreateGroup(groupName, desc, allMembers, reason, option,
                new EMValueCallBack<EMGroup>() {
                    @Override
                    public void onSuccess(EMGroup value) {
                        success.postValue("创建群成功");
                    }

                    @Override
                    public void onError(int error, String errorMsg) {
                        failed.postValue(errorMsg);
                    }
                });
    }

    public void findGroup(int pageSize, String cursor, MutableLiveData<List<EMGroupInfo>> success,
                          MutableLiveData<String> failed) {
        emClient.groupManager().asyncGetPublicGroupsFromServer(pageSize, cursor,
                new EMValueCallBack<EMCursorResult<EMGroupInfo>>() {
                    @Override
                    public void onSuccess(EMCursorResult<EMGroupInfo> value) {
                        success.postValue(value.getData());
                    }

                    @Override
                    public void onError(int error, String errorMsg) {
                        failed.postValue(errorMsg);
                    }
                });
    }

    public void joinGroup(String groupId, MutableLiveData<String> success, MutableLiveData<String> failed) {
        emClient.groupManager().asyncJoinGroup(groupId, new EMCallBack() {
            @Override
            public void onSuccess() {
                success.postValue("加入群成功");
            }

            @Override
            public void onError(int code, String error) {
                if (code == 601) {
                    failed.postValue("已经加入过该群,不能重复加入");
                } else {
                    failed.postValue(error);
                }
            }
        });
    }

    public void findJoinedGroup(MutableLiveData<List<EMGroup>> success, MutableLiveData<String> failed) {
        emClient.groupManager().asyncGetJoinedGroupsFromServer(new EMValueCallBack<List<EMGroup>>() {
            @Override
            public void onSuccess(List<EMGroup> value) {
                success.postValue(value);
            }

            @Override
            public void onError(int error, String errorMsg) {
                failed.postValue(errorMsg);
            }
        });
    }
}
